package mapEditor.view
{
	import asset.mapEditorPropertyItemViewAsset;
	
	import fl.controls.ComboBox;
	import fl.controls.NumericStepper;
	import fl.controls.TextInput;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mapEditor.events.MapEditorEvent;
	
	import stages.StageReflection;
	
	import utils.DisplayUtil;
	import utils.StringUtil;
	
	public class PropertyItemView extends mapEditorPropertyItemViewAsset
	{
		/**
		 *属性值类型:0=string;1=int;2=bool;
		 */		
		private var _valueType:int;
		private var _propertyName:String;
		private var _value:String;
		private var _valueInput:DisplayObject;
		private var _isEdit:Boolean;
		
		public function PropertyItemView(name:String, value:String, valueType:int, isEdit:Boolean=true)
		{
			_propertyName=name;
			if(StringUtil.isEmpty(_propertyName))_propertyName="";
			_value=value;
			if(StringUtil.isEmpty(_value))_value="";
			_valueType=valueType;
			_isEdit=isEdit;
			txtName.text=_propertyName;
			txtValue.text=_value;
			gotoAndStop(1);
			
			txtValue.mouseEnabled=false;
			if(_isEdit)
			{
				doubleClickEnabled=true;
				addEventListener(MouseEvent.DOUBLE_CLICK, __onDoubleClick);
			}
		}
		
		private function __onValueInputFocusChange(evt:FocusEvent):void
		{
			valueChange();
		}
		
		private function __onValueInputKeyDown(evt:KeyboardEvent):void
		{
			if(evt.keyCode==Keyboard.ENTER)valueChange();
		}
		
		private function __onDoubleClick(evt:MouseEvent):void
		{
			txtValue.visible=false;
			switch(_valueType)
			{
				case 0:
					_valueInput=new TextInput();
					(_valueInput as TextInput).text=_value;
					break;
				case 1:
					_valueInput=new NumericStepper();
					(_valueInput as NumericStepper).maximum=int.MAX_VALUE;
					(_valueInput as NumericStepper).minimum=int.MIN_VALUE;
					(_valueInput as NumericStepper).value=int(_value);
					break;
				case 2:
					_valueInput=new ComboBox();
					(_valueInput as ComboBox).addItem({label:"true"});;
					(_valueInput as ComboBox).addItem({label:"false"});;
					(_valueInput as ComboBox).selectedIndex=_value=="true" ? 0 : 1;
					break;
			}
			_valueInput.width=txtValue.width;
			_valueInput.x=txtValue.x;
			if(!(_valueInput is ComboBox))_valueInput.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, __onValueInputFocusChange);
			_valueInput.addEventListener(KeyboardEvent.KEY_DOWN, __onValueInputKeyDown);
			addChild(_valueInput);
		}
		
		private function valueChange():void
		{
			_valueInput.removeEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, __onValueInputFocusChange);
			_valueInput.removeEventListener(KeyboardEvent.KEY_DOWN, __onValueInputKeyDown);
			txtValue.visible=true;
			switch(_valueType)
			{
				case 0:
					_value=(_valueInput as TextInput).text;
					break;
				case 1:
					_value=(_valueInput as NumericStepper).value.toString();
					break;
				case 2:
					_value=(_valueInput as ComboBox).selectedLabel;
					break;
			}
			txtValue.text=_value;
			DisplayUtil.dispose(_valueInput);
			_valueInput=null;
			dispatchEvent(new MapEditorEvent(MapEditorEvent.PROPERTY_VALUE_CHANGE));
		}
		
		public function dispose():void
		{
			removeEventListener(MouseEvent.DOUBLE_CLICK, __onDoubleClick);
			
			if(_valueInput)
			{
				_valueInput.removeEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, __onValueInputFocusChange);
				_valueInput.removeEventListener(KeyboardEvent.KEY_DOWN, __onValueInputKeyDown);
				DisplayUtil.dispose(_valueInput);
				_valueInput=null;	
			}
			
			if(parent)parent.removeChild(this);
		}

		public function get propertyName():String
		{
			return _propertyName;
		}

		public function set propertyName(value:String):void
		{
			_propertyName = value;
		}

		public function get value():String
		{
			return _value;
		}

		public function set value(value:String):void
		{
			_value = value;
		}


	}
}